home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 015a / clock344.zip / CLKDEMO.C < prev    next >
C/C++ Source or Header  |  1993-01-31  |  10KB  |  262 lines

  1. /*****************************************************************************/
  2. /*                THE FOLLOWING PROGRAM IS THE SOLE PROPERTY OF              */
  3. /*                               RONALD Q. SMITH                             */
  4. /*             CONTAINING HIS PROPRIETARY CONFIDENTIAL INFORMATION           */
  5. /*                       COPYRIGHT RONALD Q. SMITH 1992                      */
  6. /*****************************************************************************/
  7.  
  8. /* Global data definitions                                                  */
  9.  
  10. #include <ctype.h>
  11. #include <dos.h>
  12. #include <time.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <clktyp.h>
  16. #include <ioctl.h>
  17.  
  18. /*
  19. PROGRAM:  CLKDEMO
  20.  
  21. CLKDEMO is used with CLOCK.SYS to provide a minimum set of control and
  22. display functions for handling both the DOS realtime clock and the calendar
  23. clock.  CLKDEMO is intended only to provide an example of a running program
  24. that uses all of the APIs unique to CLOCK.SYS.
  25.  
  26. INTERFACE:
  27.         CLKDEMO  [C=A|D|R|W] [/?]
  28.  
  29. If CLKDEMO is used with no operands it displays the current CONNECTED status
  30. of CLOCK.SYS and the time as obtained from both the DOS clock and the calendar
  31. clock.
  32.  
  33. Each of the arguments may appear in any order and as many times as desired
  34. except /?.  When /? is encountered, the usage message is displayed and CLKDEMO
  35. exits.  Each argument is a command and is processed as it is encountered.
  36. Thus you may perform a sequence of functions with a single call.
  37.  
  38.         CLKDEMO C=W
  39.  
  40. Connects the two clocks for writes.  Any DOS call to change the date or time,
  41. sets both clocks.
  42.  
  43.         CLKDEMO C=D
  44.  
  45. Disconnects the two clocks.  When the clocks are disconnected, any DOS call
  46. to change the date or time, only sets the DOS clock.  The calendar clock is
  47. unchanged.
  48.  
  49.         CLKDEMO C=R
  50.  
  51. Connects the two clocks for writes and periodic reads.  If more than 10
  52. seconds have elapsed since the last time the time was accessed, the calendar
  53. clock is read and the DOS clock is set to that value.
  54.  
  55.         CLKDEMO C=A
  56.  
  57. Connects the two clocks for all operations.  All reads and writes are
  58. directed to the calendar clock.  This is not recommended for most systems.
  59. Most calendar clocks are slow and only increment in whole seconds.
  60.  
  61. CLOCK /? displays the interface.  If /? appears anywhere on the command line,
  62. all other arguments are ignored.
  63. */
  64.  
  65. /* Global variables                                                         */
  66.  
  67. int handle;
  68.  
  69. int main(int argc, unsigned char *argv[])
  70.  
  71. {
  72.  
  73.         struct CLOCK_DATA data;
  74.         struct tm calendar_tm;
  75.         int connected;
  76.         struct time_zone *cur_zone;
  77.         char *timebuf;
  78.         time_t ltime;
  79.         struct dosdate_t dos_date;
  80.         struct dostime_t dos_time;
  81.         unsigned char *arg;
  82.         int iarg;
  83.         void usage(int status, unsigned char *arg);
  84.  
  85. /* Display copyright message                                                */
  86.  
  87.         printf("CLKDEMO %s copyright 1992, 1993 Ronald Q. Smith\n",version);
  88.  
  89. /* process command line tokens                                              */
  90.  
  91.         iarg = 0;
  92.         while(++iarg < argc)
  93.         {
  94.             arg = argv[iarg];
  95.             switch (toupper(arg[0]))
  96.             {
  97.                 default:
  98.                     usage(1, arg);
  99.  
  100.                 case 'C':   /* Set connection mode */
  101.                     if (arg[1] != '=')
  102.                         usage(1, arg);
  103.                     switch(toupper(arg[2]))
  104.                     {
  105.                         default:
  106.                             usage(1, arg);
  107.  
  108.                         case 'A':   /* Connect for all operations */
  109.                             connected = 3;
  110.                             break;
  111.  
  112.                         case 'D':   /* Disconnect */
  113.                             connected = 0;
  114.                             break;
  115.  
  116.                         case 'R':   /* Connect for periodic reads */
  117.                             connected = 2;
  118.                             break;
  119.  
  120.                         case 'W':   /* Connect for writes only */
  121.                             connected = 1;
  122.                             break;
  123.  
  124.                     } /* End switch(toupper(arg[2])) */
  125.  
  126.                     connec(&connected);
  127.                     break;
  128.  
  129.                 case '-':
  130.                 case '/':
  131.                     usage(arg[1] != '?', arg);
  132.  
  133.             } /* end switch() */
  134.  
  135.         } /* end while to process command line tokens */
  136.  
  137. /* Display status.  Also get here if no operands.                           */
  138.  
  139.     clksta(&data);
  140.  
  141.     printf("\n      CLOCK.SYS version : %.6s\n"
  142.            "             Clock type : %X - %s\n",
  143.         data.vers, data.clock_type, clock_name[data.clock_type]);
  144.  
  145.     if (clock_kind[data.clock_type] == 1)
  146.     {
  147.         printf("       Base I/O address : %X\n",data.ct_1);
  148.     }
  149.     else if (clock_kind[data.clock_type] == 2)
  150.     {
  151.         printf("    Base memory segment : %X\n"
  152.                "            Read offset : %X\n"
  153.                "         Write-0 offset : %X\n"
  154.                "         Write-1 offset : %X\n",
  155.             data.ct_1, data.ct_2, data.ct_3, data.ct_4);
  156.     }
  157.     else if (clock_kind[data.clock_type] == 3)
  158.     {
  159.         printf("              Base year : %d\n", data.ct_1);
  160.     }
  161.  
  162.     printf("             Connection : %s\n",
  163.         !data.connected ? "Disconnected" : ((data.connected == 1) ?
  164.         "Writes only" : ((data.connected == 2) ? "Writes and periodic reads" :
  165.         "All writes and reads")));
  166.  
  167.     cur_zone = data.mode.day_light ? &data.daylight : &data.standard;
  168.     if (cur_zone->zone[0] && (cur_zone->zone[0] != ' '))
  169.     {
  170.         if (cur_zone->offs.hour < 0)
  171.         {
  172.             cur_zone->offs.minute = -cur_zone->offs.minute;
  173.             cur_zone->offs.second = -cur_zone->offs.second;
  174.         }
  175.  
  176.         printf("              Time zone : %.32s\n"
  177.                "       Time zone offset : %.2hd:%.2hd:%.2hd\n",
  178.                cur_zone->zone, cur_zone->offs.hour, cur_zone->offs.minute,
  179.                cur_zone->offs.second);
  180.     }
  181.  
  182.     if (data.mode.disabl || data.mode.chk_back || data.mode.chk_forw)
  183.         printf("           Time changes : %s%s%s%s%s%s\n",
  184.             data.mode.disabl ? "Disabled" : "", (data.mode.disabl &&
  185.             (data.mode.chk_back || data.mode.chk_forw)) ? ", " : "",
  186.             (data.mode.chk_back || data.mode.chk_forw) ? "Limited " : "",
  187.             data.mode.chk_back ? "backward" : "",
  188.             (data.mode.chk_back && data.mode.chk_forw) ? ", " : "",
  189.             data.mode.chk_forw ? "forward" : "");
  190.  
  191.     if (data.mode.chk_back)
  192.         printf("         Backward limit : -%.2d:%.2d:%.2d\n",
  193.             data.rules.back.hour, data.rules.back.minute,
  194.             data.rules.back.second);
  195.  
  196.     if (data.mode.chk_forw)
  197.         printf("          Forward limit : +%.2d:%.2d:%.2d\n",
  198.             data.rules.forward.hour, data.rules.forward.minute,
  199.             data.rules.forward.second);
  200.  
  201.     if (data.mode.disp_tim)
  202.         printf("Continuous time display : x=%d, y=%d, attribute=%.2X, "
  203.             "%s-hour\n", data.display.x, data.display.y,
  204.             data.display.attribute, data.mode.disp_24 ? "24" : "12");
  205.  
  206.     if (data.mode.pw_ena)
  207.         printf("    Password protection : Enabled\n");
  208.  
  209.     calendar_tm.tm_sec = data.cal_time.time.second;
  210.     calendar_tm.tm_min = data.cal_time.time.minute;
  211.     calendar_tm.tm_hour = data.cal_time.time.hour;
  212.     calendar_tm.tm_mday = data.cal_time.day;
  213.     calendar_tm.tm_mon = data.cal_time.month - 1;
  214.     calendar_tm.tm_year = data.cal_time.year - 1900;
  215.     calendar_tm.tm_wday = 0;
  216.     timebuf = asctime(&calendar_tm);
  217.  
  218.     printf("          Calendar time : %s",&timebuf[4]);
  219.  
  220.     time(<ime);
  221.     timebuf=ctime(<ime);
  222.     printf("               DOS time : %s",&timebuf[4]);
  223.  
  224.         return(0);
  225. }
  226.  
  227. /*
  228. PROCEDURE: usage
  229.  
  230. "usage" displays the interface to CLKDEMO.  It is called when the /? option is
  231. used on the command line or when any invalid argument appears on the command
  232. line.  A program exit with a code of status occurs.
  233. */
  234.  
  235. void usage(int status, unsigned char *arg)
  236.  
  237. {
  238.         if (status)
  239.             printf("\nIncorrect argument format: %s\n", arg);
  240.  
  241.         printf("\nCLKDEMO usage:\n\n"
  242.                "          CLKDEMO [C=A|D|R|W] [/?]\n\n"
  243.                "CLKDEMO     - displays the connected status and \n"
  244.                "              the current DOS and calendar clock times.\n"
  245.                "CLKDEMO C=A - Connects the DOS and calendar clocks\n"
  246.                "              for all operations.\n"
  247.                "CLKDEMO C=D - Disconnects the clocks.\n"
  248.                "CLKDEMO C=R - Connects the DOS and calendar clocks for\n"
  249.                "              writes and periodic reads.\n"
  250.                "CLKDEMO C=W - Connects the DOS and calendard clocks for"
  251.                " writes.\n"
  252.                "CLKDEMO /? - displays this message.\n");
  253.         exit(status);
  254. }
  255.  
  256. /*****************************************************************************/
  257. /*                THE PRECEDING PROGRAM IS THE SOLE PROPERTY OF              */
  258. /*                               RONALD Q. SMITH                             */
  259. /*             CONTAINING HIS PROPRIETARY CONFIDENTIAL INFORMATION           */
  260. /*                       COPYRIGHT RONALD Q. SMITH 1992                      */
  261. /*****************************************************************************/
  262.